<?php
class Site_Model_Products
{
	public $detail;
	public $product;
	public $stock;
	public $sizes;
	public $colors;
	public $image;
	public $gallery;
	
	public function __construct()
	{
	}
	
	public function getNews($limit)
	{
		$product = new Application_Model_Db_ProductsDetails();
		$select = $product->select()->where( 'idFlag = 2' )->limit($limit);
		
		$products = $product->fetchAll( $select );
		
		return $products;
	}
	
	public function getProducts($limit, array $filters = null)
	{
		$product = new Application_Model_Db_ProductsDetails();
		$select = $product->select()->limit($limit);
		
		if( isset( $filters['id'] ) && $filters['id'] != 'promocao' )
		{
			$category = new Site_Model_Category();
			$category = $category->getCategoryId( $filters['id'] );
			$select->where( 'idCategory = ' . $category );
		}
		if( isset( $filters['gender'] ) && $filters['gender'] && $filters['gender'] != 'todos' )
		{
			$select->where( 'genderTags LIKE "%' . $filters['gender'] . '%"' );
		}
		if( isset( $filters['id'] ) && $filters['id'] == 'promocao' )
		{
			$select->where( 'idFlag = 4' )->orWhere( 'idFlag = 5' );
		}
		if( isset( $filters['flag'] ) && $filters['flag'] != '0' && $filters['flag'] != '' )
		{
			$select->where( 'idFlag = ' . $filters['flag'] );
		}
		if( isset( $filters['theme'] ) && $filters['theme'] != '0' && $filters['theme'] != '' )
		{
			$select->where( 'themeTags  LIKE "%' . $filters['theme'] . '%"' );
		}
		
		$products = $product->fetchAll( $select );

		return $products;
	}
	
	public function getPromotions($limit)
	{
// 		$product = new Application_Model_Db_ProductsDetails();
// 		$select = $product->select()->where( 'idFlag = 4' )->limit($limit);
		
// 		$products = $product->fetchAll( $select );
		
		return $this->getProducts($limit, array( 'id' => 'promocao') );
	}
	
	public function getProduct($id)
	{
		$detail = new Application_Model_Db_Details();
		$stock = new Application_Model_Db_Stock();
		$sizes = new Site_Model_Sizes();
		$colors = new Site_Model_Colors();
		
		$stockSelect = $stock->select()->where('quantity >= 1');
		
		$rowDetail = $detail->find( $id )->current();
		$rowProduct = $rowDetail->findDependentRowset('Application_Model_Db_Products')->current();
		$rowStock = $rowDetail->findDependentRowset('Application_Model_Db_Stock',null, $stockSelect);
		$rowOtherColors = $rowProduct->findDependentRowset('Application_Model_Db_Details');
		
		$this->detail = $rowDetail;
		$this->product = $rowProduct;
		$this->stock = $rowStock;
		$this->otherColors = $rowOtherColors;
		$this->sizes = $sizes->getSizesMatrix();
		$this->colors = $colors->getColorsMatrix();
		$this->gallery = $this->getDetailImages( $this->detail->folder );
		
		return $this;
	}
	
	public function getDetailImages($folder)
	{
		$this->image = '';
		$folder = __ROOT__ . 'public/uploads/' . $folder . '/';
		$file = str_replace( '//','/', $_SERVER["DOCUMENT_ROOT"] . $folder . 'info.inf' );
		
		if( is_file( $file ) )
		{
			foreach( Zend_Json_Decoder::decode( Nadeb_Savefile::get($file) ) as $key => $value )
			{
				$file = $value[ 'file' ];
				$this->image[ $key ]->file = $file;
				
				$oldFolder = str_replace( '//','/', $_SERVER['DOCUMENT_ROOT'] . $folder );
				$newFolder = str_replace( '//','/', $_SERVER['DOCUMENT_ROOT'] . __ROOT__ . 'public/uploads/cache/' );
				
				if( !is_file( $newFolder . 'tm01-' . $file ) ) 
					$this->createImage($oldFolder, $newFolder, $folder, $file);
			}
		}
		
		return $this->image;
	}
	
	private function createImage($oldFolder, $newFolder, $folder, $file)
	{
		@mkdir($newFolder, 0777, true);
		@chmod($newFolder, 0777, true);
			
		$image = new Nadeb_CropImages();
		$image->set_patch( $folder )
			  ->set_file( $file )
			  ->set_newName( 'tm01-' . $file )
			  ->set_newSize('495','540')
			  ->create()
			  ->crop_by_width()
// 			  ->auto_crop()
			  ->make_file();
		
		@chmod($oldFolder . 'tm01-' . $file, 0777, true);
		@rename($oldFolder . 'tm01-' . $file, $newFolder . 'tm01-' . $file);
	}
	
}


